package cn.gson.springboot.model.service;

import cn.gson.springboot.model.dao.ContractDao;
import cn.gson.springboot.model.dao.ProductDao;
import cn.gson.springboot.model.dao.ProductDetailDao;
import cn.gson.springboot.model.mapper.ContractMapper;
import cn.gson.springboot.model.pojos.Contract;
import cn.gson.springboot.model.pojos.ProductDetail;
import cn.gson.springboot.model.vo.ContractVo;
import cn.gson.springboot.model.vo.ProductDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ContractService {
    @Autowired
    ContractMapper contractMapper;
    @Autowired
    ContractDao contractDao;
    @Autowired
    ProductDao productDao;
    @Autowired
    ProductDetailDao productDetailDao;
    @Autowired
    ProductDetailService productDetailService;


    /*public List<Contract> selectEmpContract(Integer deptId){
        return contractMapper.selectEmpContract(deptId);
    }*/
    public Contract selectEmpContractById(Integer conId){
        return contractMapper.selectEmpContractById(conId);
    }

    public List<Contract> selectEmpContractByStatus(Integer deptId,Integer contractStatus){
        return contractMapper.selectEmpContractByStatus(deptId,contractStatus);
    }
    public List<Contract> selectEmpContractByClassify(Integer deptId,Integer contractSort){
        return contractMapper.selectEmpContractByClassify(deptId,contractSort);
    }
    public List<Contract> selectEmpContractByTheme(Integer deptId,String contractTheme){
        return contractMapper.selectEmpContractByTheme(deptId,contractTheme);
    }
    public List<Contract> selectEmpContractByCustomerName(Integer deptId,String customerName){
        return contractMapper.selectEmpContractByCustomerName(deptId,customerName);
    }

    public List<Map<String,Object>> computeSorts(Integer deptId){
        return contractMapper.computeSorts(deptId);
    }

    public List<Map<String,Object>> computeStyle(Integer deptId){
        return contractMapper.computePayStyle(deptId);
    }

    public List<Map<String,Object>> computeSignDate(Integer deptId){
        return contractMapper.computeSignDate(deptId);
    }

    public List<Map<String,Object>> computeEndDate(Integer deptId){
        return contractMapper.computeEndDate(deptId);
    }

    public List<Map<String,Object>> computeConStatus(Integer deptId){
        return contractMapper.computeConStatus(deptId);
    }

    @Transactional(transactionManager = "transactionManager")
    public Integer saveContract(ContractVo vo){
        System.out.print("555555");
        //先增主表
        Contract con = new Contract();
        con.setContractId(vo.getContractId());
        con.setContractTheme(vo.getContractTheme());
        con.setContractTotalAmount(vo.getContractTotalAmount());
        con.setContractBackAmount(vo.getContractBackAmount());
        con.setContractStatus(vo.getContractStatus());
        con.setContractEmpName(vo.getContractEmpName());
        con.setContractSort(vo.getContractSort());
        con.setContractStartTime(vo.getContractStartTime());
        con.setContractEndTime(vo.getContractEndTime());
        con.setContractCustomerSigner(vo.getContractCustomerSigner());
        con.setContractOurSigner(vo.getContractOurSigner());
        con.setContractPaymentMethods(vo.getContractPaymentMethods());
        con.setContractMoneyMethods(vo.getContractMoneyMethods());
        con.setContractBodyText(vo.getContractBodyText());
        con.setContractNo(vo.getContractNo());
        con.setContractInvoiceAmount(vo.getContractInvoiceAmount());
        con.setContractProductService(vo.getContractProductService());
        con.setContractChance(vo.getContractChance());
        con.setContractNote(vo.getContractNote());
        con.setEmployeeByEmpId(vo.getEmployeeByEmpId());
        con.setCustomerByCustomerId(vo.getCustomerByCustomerId());
        con.setOpportunityByOppId(vo.getOpportunity());
        con.setContractAppStatus(vo.getContractAppStatus());
        con.setEmployeeByAppEmpId(vo.getEmployeeByAppEmpId());
        contractDao.save(con);

        //增详情单
        //增详单
        List<ProductDetailsVo> details = vo.getDetails();

        productDetailDao.deleteByConId(con.getContractId());
        System.out.println("xxx"+details);
        for (ProductDetailsVo d : details){
            //分别创建详单对象
            ProductDetail pd = new ProductDetail();
            pd.setProductDetailId(0);
            pd.setProductDetailRemarks(d.getProductDetailRemarks());
            pd.setProductDetailName(d.getProductDetailName());
            pd.setProductDetailModel(d.getProductDetailModel());//设置所属订单
            pd.setProductDetailCount(d.getProductDetailCount());
            pd.setProductDetailPrice(d.getProductDetailPrice());
            pd.setProductDetailMoney(d.getProductDetailMoney());
            pd.setProductDetailTotalMoney(d.getProductDetailTotalMoney());
            pd.setProductByProductId(d.getProductByProductId());
            pd.setProductDetailOutNum(d.getProductDeliverCount());
            pd.setContractByContractId(con);
            productDetailDao.save(pd);
        }
        return con.getContractId();
    }

    public void deleteContract(Integer contractId){
        contractDao.deleteById(contractId);
    }

    public List<Contract> findAllContractTheme(){
        return contractMapper.findAllContractTheme();
    }

    public List<Contract> selectCusContract(Integer cusId) {
        return  contractMapper.selectCusContract(cusId);
    }

    public List<Contract> findAllValidContract(Integer deptId) {
        return  contractMapper.findAllValidContract(deptId);
    }

    public List<Contract> findAllAppContract(int deptId) {
        return contractMapper.findAllAppContract(deptId);
    }

    public List<Contract> findAllRefuseContract(int deptId) {
        return contractMapper.findAllRefuseContract(deptId);
    }

    public List<Contract> findAllOurSigner() {
        return contractMapper.findAllOurSigner();
    }

    public List<Contract> selectEmpContract(int deptId, int empId, Integer value, Integer num, Integer vague, String content) {
        return contractMapper.selectEmpContract(deptId,empId,value,num,vague,content);
    }

    public Contract selectContractByConNo(String conNo) {
        return contractMapper.selectContractByConNo(conNo);
    }

    public List<Contract> selectContractImplement() {
        return contractMapper.selectContractImplement();
    }

    public List<Contract> selectContractPastThirty() {
        return contractMapper.selectContractPastThirty();
    }

    public List<Contract> selectContractNotPayment() {
        return contractMapper.selectContractNotPayment();
    }
}
